package com.example.forum.services.impl;

import com.example.forum.dao.ArticleMapper;
import com.example.forum.model.Article;
import com.example.forum.model.ArticleReply;
import com.example.forum.services.ArticleService;
import com.example.forum.services.BoardService;
import com.example.forum.services.UserService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

//service层的实现类,也会涉及到业务代码
@Service
public class ArticleServiceImpl implements ArticleService {
    @Resource
    ArticleMapper articleMapper;
    @Resource
    private UserService userService;
    @Resource
    private BoardService boardService;

    /**
     * 首页查询所有文章————Session
     * @return
     */
    @Override
    public List<Article> selectAll() {
        return articleMapper.selectAll();
    }

    /**
     * 展示某一板块所有文章————Session、板块id
     * @param boardId
     * @return
     */
    @Override
    public List<Article> selectByBoardId(Long boardId) {
        return articleMapper.selectByBoardId(boardId);
    }

    /**
     * 展示用户所有文章————Session、用户id
     * @param userId
     * @return
     */
    @Override
    public List<Article> selectByUserId(Long userId) {
        return articleMapper.selectByUserId(userId);
    }

    /**
     * 发布文章————Session
     * @param article
     */
    @Override
    public void create(Article article) {
        //设置默认值
        article.setVisitCount(0); //访问数
        article.setReplyCount(0); //回复数
        article.setLikeCount(0);  //点赞数
        article.setDeleteState((byte) 0);
        article.setState((byte) 0);
        Date date = new Date();
        article.setCreateTime(date);
        article.setUpdateTime(date);
        //添加操作
        articleMapper.insertSelective(article);
        //如果要在这里面判断ret是否为1，要写异常

        //文章技既属于用户，也是与某个板块
        //所以对应的数量需要增加！

        //增加用户文章数量
        userService.addOneArticleCountById(article.getUserId());
        //增加板块文章数量
        boardService.addOneArticleCountById(article.getBoardId());
    }

    /**
     * 查看文章详情————Session、文章id
     * @param id
     * @return
     */
    @Override
    public Article selectDetailById(Long id) {
        //访问次数加一
        Article article = articleMapper.selectDetailById(id);
        article.setVisitCount(article.getVisitCount()+1);
        articleMapper.updateByPrimaryKeySelective(article);
        return article;
    }

    /**
     * 给文章点赞————Session、文章id
     * @param id
     */
    @Override
    public void thumbsUpById(Long id) {
        Article article = articleMapper.selectByPrimaryKey(id);
        article.setLikeCount(article.getLikeCount()+1);
        articleMapper.updateByPrimaryKeySelective(article);

    }

    /**
     * 展示评论列表————Session、文章id
     * @param articleId
     * @return
     */
    @Override
    public List<ArticleReply> selectReplyByArticleId(Long articleId) {
        return articleMapper.selectByArticleID(articleId);
    }

    /**
     * 获取文章————Session、文章id(系统内部使用,仍需Session,防止非法途径获取文章)
     * @param id
     * @return
     */
    @Override
    public Article selectById(Long id) {
        return articleMapper.selectByPrimaryKey(id);
    }

    /**
     * 文章阅读量增加————Session、文章id(系统内部使用,仍需Session,防止非法途径获取文章)
     * @param articleId
     */
    @Override
    public void addReplyCount(Long articleId) {
        Article article = articleMapper.selectByPrimaryKey(articleId);
        article.setReplyCount(article.getReplyCount()+1);
        articleMapper.updateByPrimaryKeySelective(article);
    }

    /**
     * 编辑修改文章————Session、文章id、新标题、新正文
     * @param id
     * @param title
     * @param content
     */
    @Override
    public void modify(Long id, String title, String content) {
        Article article = articleMapper.selectByPrimaryKey(id);
        if(title!=null)
            article.setTitle(title);
        if(content!=null)
            article.setContent(content);
        Date date = new Date();
        article.setUpdateTime(date);
        System.out.println(article);
        articleMapper.updateByPrimaryKeySelective(article);
    }

    /**
     * 删除文章————Session、文章id
     * @param id
     */
    @Override
    public void deleteById(Long id) {
        Article article = articleMapper.selectByPrimaryKey(id);
        article.setDeleteState((byte) 1);
        //删除文章
        articleMapper.updateByPrimaryKeySelective(article);
        //更新板块文章
        userService.subOneArticleCountById(article.getUserId());
        //更新用户文章数量
        boardService.subOneArticleCountById(article.getBoardId());
    }

}
